package com.example.chao.myphotosearch.presenter;

import com.example.chao.myphotosearch.api.HttpRetrofit;
import com.example.chao.myphotosearch.api.HttpSubscribe;
import com.example.chao.myphotosearch.bean.SosoImage;
import com.example.chao.myphotosearch.interfaces.SubscriberDoOnNextListener;
import com.example.chao.myphotosearch.view.fragments.NetImgFragment;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by chao on 2016/4/8.
 */
public class NetImgFragmentPresenter implements IPresenter<NetImgFragment> {
    private NetImgFragment iView;
    private int page = 1;
    private String word;
    List<SosoImage> sosoImages;
    //是否正在加载
    private boolean isLoading = false;

    @Inject
    public NetImgFragmentPresenter(){
        sosoImages = new ArrayList<>();
    }

    @Override
    public void onCreate(NetImgFragment iView) {
        this.iView = iView;
        word = iView.getArguments().getString("title");
        onViewCreated();
    }
    public void loadMore(){
        if(!isLoading) {
            page++;
            onViewCreated();
        }
    }
    @Override
    public void onViewCreated() {
        isLoading = true;
        HttpRetrofit.getImageList(word, page, new HttpSubscribe<List<SosoImage>>(new SubscriberDoOnNextListener<List<SosoImage>>() {
            @Override
            public void doOnNext(List<SosoImage> images) {
                sosoImages.addAll(images);
                iView.attachView(sosoImages);
                isLoading = false;
            }
        }));
        iView.hideRefresh();
    }
}
